package com.epam.rentcar.controller.actions;

import java.sql.SQLException;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.epam.rentcar.model.CarRent;

public class CancellRequestAction implements Action {
	
	private static Logger log = Logger.getLogger(CancellRequestAction.class);
	
	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) throws ServletException {
		
		CarRent carRentAppModel = (CarRent) request.getSession().getAttribute("carRentAppModel");
		
		int id = Integer.parseInt(request.getParameter("id"));
		int carID = Integer.parseInt(request.getParameter("carID"));
		String cancellationPurpose = request.getParameter("cancellationPurpose");
		
		try {
			carRentAppModel.cancellRequest(id, cancellationPurpose);
			carRentAppModel.makeCarAvaliable(carID);
		} catch (SQLException | NamingException e) {
			log.error(e);
			throw new ServletException(e);
		}
		
		return "/adminPage.jsp";
	}

}
